package com.atguigu.tingshu.album.service.impl;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<CategoryVo> getBaseCategoryList() {
		return baseCategory1Mapper.getBaseCategoryList();
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.findAttribute(category1Id);
	}

	@Override
	public BaseCategoryView getAlbumCategory(Long albumId) {
		return baseCategoryViewMapper.getAlbumCategory(albumId);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//根据一级分类id 查询一级分类的二级分类集合
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getIsDeleted,0);
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);

		if (baseCategory2s == null){
			throw new GuiguException(201,"该一级分类下不存在二级分类");
		}

		List<Long> baseCategory2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());


		LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id,baseCategory2Ids);
		baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop,1);
		baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsDeleted,0);
		baseCategory3LambdaQueryWrapper.last("limit 7");
		List<BaseCategory3> category3s = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);

		return category3s;
	}

	@Override
	public CategoryVo getBaseCategoryListByCategory1Id(Long category1Id) {



		//1.根据一级分类id查询一级分类对象
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
		if (baseCategory1 == null){
			throw new GuiguException(201,"该一级分类对象不存在");
		}

		CategoryVo categoryVo1 = new CategoryVo(); //一级分类对象
		categoryVo1.setCategoryId(category1Id);
		categoryVo1.setCategoryName(baseCategory1.getName());

		//根据一级分类对象查询二级分类集合
		LambdaQueryWrapper<BaseCategory2> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		lambdaQueryWrapper.eq(BaseCategory2::getIsDeleted,0);

		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(lambdaQueryWrapper);
		if (CollectionUtils.isEmpty(baseCategory2s)){
			throw new GuiguException(201,"该一级分类下不存在二级分类集合");
		}

		ArrayList<CategoryVo> category1Child = new ArrayList<>();

		for (BaseCategory2 baseCategory2 : baseCategory2s) {
			CategoryVo category2Vo = new CategoryVo();  //二级分类对象
			category2Vo.setCategoryId(baseCategory2.getCategory1Id());
			category2Vo.setCategoryName(baseCategory2.getName());



			LambdaQueryWrapper<BaseCategory3> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
			lambdaQueryWrapper1.eq(BaseCategory3::getCategory2Id,baseCategory2.getId());
			lambdaQueryWrapper1.eq(BaseCategory3::getIsDeleted,0);

			List<BaseCategory3> category3s = baseCategory3Mapper.selectList(lambdaQueryWrapper1);
			if (CollectionUtils.isEmpty(category3s)){
				throw new GuiguException(201,"该二级分类下不存在三级分类集合");
			}
			ArrayList<CategoryVo> category2Child = new ArrayList<>();
			for (BaseCategory3 category3 : category3s) {
				CategoryVo category3Vo = new CategoryVo();
				category3Vo.setCategoryId(category3.getId());
				category3Vo.setCategoryName(category3.getName());

				category2Child.add(category3Vo);
			}

			category2Vo.setCategoryChild(category2Child);

			category1Child.add(category2Vo);

		}

		categoryVo1.setCategoryChild(category1Child);

		return categoryVo1;

	}

	@Override
	public List<Long> getAllCategory1Id() {
		List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
        return baseCategory1s.stream().map(baseCategory1 -> baseCategory1.getId()).collect(Collectors.toList());
	}








	/*@Override
	public List<CategoryVo> getBaseCategoryList() {

		List<CategoryVo> result = new ArrayList<>();


		//1.查询所有的分类信息
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);

		//2.封装数据
		Map<Long, List<BaseCategoryView>> category1IdAndValue = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : category1IdAndValue.entrySet()) {
			//1.定义一个一级分类
			CategoryVo category1Vo = new CategoryVo();
			category1Vo.setCategoryId(longListEntry.getKey()); //给一级分类id赋值
			category1Vo.setCategoryName(longListEntry.getValue().get(0).getCategory1Name()); //给一级分类名字赋值

			Map<Long, List<BaseCategoryView>> category2IdAndValue = longListEntry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			ArrayList<CategoryVo> category1Child = new ArrayList<>();
			for (Map.Entry<Long, List<BaseCategoryView>> listEntry : category2IdAndValue.entrySet()) {
				//2.定义一个二级分类
				CategoryVo category2Vo = new CategoryVo();
				category2Vo.setCategoryId(listEntry.getKey()); //给二级分类id赋值
				category2Vo.setCategoryName(listEntry.getValue().get(0).getCategory2Name());  //给二级分类名字赋值
				ArrayList<CategoryVo> category2Child = new ArrayList<>();
				for (BaseCategoryView categoryView : listEntry.getValue()) {
					CategoryVo category3Vo = new CategoryVo();
					category3Vo.setCategoryId(categoryView.getCategory3Id());
					category3Vo.setCategoryName(categoryView.getCategory3Name());
					category2Child.add(category3Vo);
				}


				category2Vo.setCategoryChild(category2Child); //给二级分类的孩子赋值
				category1Child.add(category2Vo);
			}

			category1Vo.setCategoryChild(category1Child); //给一级分类的孩子赋值

			result.add(category1Vo); //将一级分类放入到结果集中
		}


		return result;
	}*/
}
